package com.zl.basic.controller;

import com.zl.basic.util.AjaxResult;
import com.zl.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    /**
     * 上传接口
     * @param
     * @return
     */
    @PostMapping

    public AjaxResult upload(@RequestPart MultipartFile file){
        try {
            String originalFilenam = file.getOriginalFilename();
            String extName = originalFilenam.substring(originalFilenam.lastIndexOf(".")+1);

            String filename = FastDfsUtil.upload(file.getBytes(), extName);

            return AjaxResult.me().setResultObj(filename);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }
    }

    /**
     * 删除接口
     * @param
     * @return
     */
    @DeleteMapping
    public AjaxResult del(@RequestParam String path){
        try {
            String pathTem = path.substring(1);
            String group = pathTem.substring(0,pathTem.indexOf("/"));
            String filePath =  pathTem.substring(pathTem.indexOf("/")+1);

            FastDfsUtil.delete(group,filePath);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


}
